from selenium import webdriver
import unittest
import time

class search2(unittest.TestCase):
    # 测试固件
    def setUp(self):
        print("------setUp------")
        self.driver = webdriver.Chrome()
        self.url = "http://127.0.0.1:8080/"
        self.driver.maximize_window()
        time.sleep(3)

    def tearDown(self):
        print("------tearDown------")
        self.driver.quit()

    # 点击上一页能否正常跳转
    def test_search1(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_xpath("/html/body/div[1]/div/input").click()
        driver.find_element_by_xpath("/html/body/div[1]/div/input").send_keys("string")
        driver.find_element_by_xpath("/html/body/div[1]/i[2]").click()
        time.sleep(3)
        # 新窗口的定位
        windows = driver.window_handles
        driver.switch_to.window(windows[1])
        js0 = "var q=document.documentElement.scrollTop=10000"
        driver.execute_script(js0)
        driver.find_element_by_xpath("/html/body/div[3]/a[1]").click()
        # 获取当前页面的url
        currentPageUrl = self.driver.current_url
        # 断言当前网页的网址是否为正确的url
        self.assertEqual(currentPageUrl, "http://127.0.0.1:8080/web?query=string&page=0", "当前网页网址符合预期！")
        time.sleep(3)

    # 点击下一页能否正常跳转
    def test_search2(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_xpath("/html/body/div[1]/div/input").click()
        driver.find_element_by_xpath("/html/body/div[1]/div/input").send_keys("string")
        driver.find_element_by_xpath("/html/body/div[1]/i[2]").click()
        time.sleep(3)
        # 新窗口的定位
        windows = driver.window_handles
        driver.switch_to.window(windows[1])
        js0 = "var q=document.documentElement.scrollTop=10000"
        driver.execute_script(js0)
        driver.find_element_by_xpath("/html/body/div[3]/a[2]").click()
        # 获取当前页面的url
        currentPageUrl = self.driver.current_url
        # 断言当前网页的网址是否为正确的url
        self.assertEqual(currentPageUrl, "http://127.0.0.1:8080/web?query=string&page=2", "当前网页网址符合预期！")
        time.sleep(3)

    # 点击标题，跳转到对应的html
    def test_search3(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_xpath("/html/body/div[1]/div/input").click()
        driver.find_element_by_xpath("/html/body/div[1]/div/input").send_keys("string")
        driver.find_element_by_xpath("/html/body/div[1]/i[2]").click()
        time.sleep(3)
        # 新窗口的定位
        windows = driver.window_handles
        driver.switch_to.window(windows[1])
        driver.find_element_by_xpath("/html/body/div[1]/div/a").click()
        time.sleep(3)
        self.assertEqual("蜡笔搜索", driver.title, msg="跳转成功")

    # 在详情页点击蜡笔搜索
    def test_search4(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_xpath("/html/body/div[1]/div/input").click()
        driver.find_element_by_xpath("/html/body/div[1]/div/input").send_keys("string")
        driver.find_element_by_xpath("/html/body/div[1]/i[2]").click()
        time.sleep(3)
        windows = driver.window_handles
        driver.switch_to.window(windows[1])
        driver.find_element_by_xpath("/html/body/div[1]/form/input").clear()
        time.sleep(3)
        driver.find_element_by_xpath("/html/body/div[1]/form/input").send_keys("map")
        driver.find_element_by_xpath("/html/body/div[1]/form/button").click()
        time.sleep(3)
        self.assertEqual("map - 蜡笔搜索", driver.title, msg="搜索成功")
